package com.jiaozi.core.config.mybatis;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.jiaozi.core.security.utils.SecurityUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;

@Slf4j
public class MybatisMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        if(getFieldValByName("createdBy",metaObject)==null){
            if(SecurityUtil.getLoginUser()!=null){
                setFieldValByName("createdBy", SecurityUtil.getLoginUser().getLoginUser().getUserName(),metaObject);
            }
        }
        if(getFieldValByName("updatedBy",metaObject)==null){
            if(SecurityUtil.getLoginUser()!=null){
                setFieldValByName("updatedBy", SecurityUtil.getLoginUser().getLoginUser().getUserName(),metaObject);
            }
        }

    }

    @Override
    public void updateFill(MetaObject metaObject) {
        if(getFieldValByName("updatedBy",metaObject)==null){
            if(SecurityUtil.getLoginUser()!=null){
                setFieldValByName("updatedBy", SecurityUtil.getLoginUser().getLoginUser().getUserName(),metaObject);
            }
        }
    }
}
